<?

/*	CalendarBasic OBJECT
*	Comment here
*	
*					*/


class CalendarBasic extends MOD {

	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	var $days =		array(
		"nl" =>		array( 0, "zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag" )
	);
	
	public function construct() {
		global $DB, $CAL, $Page, $Cal;
		
		$CAL = new CALENDAR( "%d.%m %y" );

		$Cal = new CALNODE( $this->vars->type, (int) $_GET['calid'] );
		$Page->set_cache( "title", date( "d.m Y", strtotime( $Cal->get( 'date' ))));
	}

	
	public function output() {
	
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
	
		
		if( $nodes ) {
			$this->addclass( $this->action );
			$result = ( $nodes ) ?
				"<ul>" . implode("\n", $nodes ) . "</ul>\n" . $script : "empty.";
		
			return $this->parse( $result );
		}
	
	}
	
	
	private function filter_visual() {
		global $DB, $CAL;
		
		if( !$CAL ) $CAL = new CALENDAR();
		
		if( $dates = $CAL->filter_nodes( "now()", 128, $this->vars->type, $this->vars->objtype, false, $this->vars->limit )) {
		
			foreach( $dates as $Node )
				if( $Node->obj->hasMedia())
					$nodes[] = $this->render_li( $name = $Node->obj->get( "name" ), $Node->obj->uri(), "<img src='/public.img?id=" . $Node->obj->media->get( "media.img" )->ID . "&sze={$this->vars->imgsize}' alt='$name' />" );
		
			return $nodes;
		}
	}
	
	
	private function filter_dayview() {
		global $DB, $CAL, $Engine, $Cal;
		
		if( $nodes = $CAL->filter_nodes( $CAL->smartday( $Cal->get( "date" )), 1 )) 
			foreach( $nodes as $node ) {
			
				$text = ( $id = $node->get( 'textid' ))? $Engine->text->get( $id, "long" ) : "";
			
				if( $node->get( "objid" ) ) {
					$content = "<span class='time'>{$node->get( 'timestr' )}</span><a class='name' href='{$node->obj->url()}'>{$node->obj->get( 'name' )}</a><span class='loc'>{$node->loc->get( 'name' )}</span><span class='descr'>$text</span>";
					$lines[] = ( $with_nfo )? "<li class='obj'>$content</li>" : "<li class='none'>$content<span class='cont'>" . substr( $Engine->text->get( $node->obj->get( "textid" ), "long" ), 0, 142 ). " ...</span></li>";
				} else {
					$with_nfo = true;
					$lines[] = "<li class='nfo'><h1>{$node->get( 'name' )}</h1>$text</li>";
				}
				
					
			}
		
		$this->nodes = $nodes;
		return $lines;
	}	
	

	
	
	private function filter_daylist() {
		global $DB, $CAL, $Engine, $Cal;
		
		if( $nodes = $CAL->filter_nodes( $CAL->smartday( $Cal->get( "date" )), 1 )) {
		
			$this->set_title( $nodes[ 0 ]->get( "datestr" ));
		
			$lines[] = "<li class='descr'>". $this->days[ LANG ][ $nodes[ 0 ]->get( "day" )] ."</li>";
		
			foreach( $nodes as $node ) if( $node->obj ) {
				
				$class = ( $node->obj->type == $DB->type( $this->vars->secondary ))? "second" : "";
				$class.= ( $node->id == $Cal->id )? "active" : "";
				
				$lines[] = "<li class='$class'><a href='{$node->obj->uri()}'>{$node->obj->get( 'name' )}</a></li>";
			}

		
		
		}
		
		/*$content[] = "<li class='descr'>". $this->days[ LANG ][ $this->calnode[ 'day' ]] ."</li>";
		
		foreach( $this->calnodes as $node ) {
			if( $node['objtype'] != $DB->type( $this->vars->secondary )) $content[] =  "<li ".(( $node['ID'] == $this->calnode['ID'] )? "class='active'":"" )."><a href='?calid={$node[ 'ID' ]}'>{$node[ 'objname' ]}</a></li>";
			else $secondary[] = "<li class='second".(( $node['ID'] == $this->calnode['ID'] )? " active":"" )."'><a href='?calid={$node[ 'ID' ]}'>{$node[ 'objname' ]}</a></li>";
		}
		
		if($secondary) $content = array_merge( $content, $secondary );
		
		$older = $DB->get( $this->vars->type, "date(date) < date( '{$this->calnode['date']}' ) order by date desc", "ID, date_format( date, '%d') as daym" );
		$newer = $DB->get( $this->vars->type, "date(date) > date( '{$this->calnode['date']}' ) order by date asc", "ID, date_format( date, '%d') as daym" );
		
		$content[] = "<li class='nav'><span><a href='?calid={$older['ID']}'>{$older['daym']}</a></span><span>{$this->calnode['daym']}</span><span><a href='?calid={$newer['ID']}'>{$newer['daym']}</a></span></li>";
		
		*/
		return $lines;
	}


	private function filter_upcoming() {
		global $DB, $Page;
		
		if( is_array( $this->vars->objtype)) $this->vars->objtype = implode( ",", $this->vars->objtype ); 
		
		$response = $DB->filter_key(
			$this->vars->type, 
			"date > now() && otype in({$this->vars->objtype}) && active order by mod_calendar.date asc limit " . $this->vars->limit,
			"ID, otype, objid, date_format(date, '{$this->vars->dformat}') as date, (select <%lang:obj.nameid%> from mod_object as obj where obj.ID=objid) as name"
		);
		
		/////// TEMP!
		$uri = "/agenda/dag";
			
		if( $response ) foreach( $response  as $day ) {
			
			/*if( !$uri[ $day['otype'] ] ) {
				$meta = $DB->type_r( $day['otype'], "meta" );
				$page = new PAGE( (int) json_decode($meta)->pageid );
				$uri[ $day['otype'] ] = $page->parse_uri();
			}
			$readable = $uri[ $day['otype'] ] . "?id={$day['objid']}&amp;calid={$day['ID']}&amp;name=". urlencode( $day['name'] );*/
			
			$readable = "$uri?id={$day['objid']}&amp;calid={$day['ID']}&amp;name=". urlencode( $day['name'] );
			
			$nodes[] = "<li><a href='$readable' class='name'>{$day['name']}</a><span class='date'>{$day['date']}</span></li>";
		}
		
		return $nodes;
	}	
	
	
	private function filter_current() {
		global $DB, $Object;
		
		if( is_array( $this->vars->objtype)) $this->vars->objtype = implode( ",", $this->vars->objtype ); 
		
		$yesterday 	= date( 'Y-m-d H:i:s', strtotime( 'yesterday' ));
		$now		= date( 'Y-m-d H:i:s', time());
		//$now		= '2010-10-13 22:10:00';
		$response 	= $DB->filter_key(
			$this->vars->type, 
			"date > '" . $yesterday . "' && otype in({$this->vars->objtype}) && active order by mod_calendar.date asc limit 25",
			"ID, otype, objid, span, date, (select <%lang:obj.nameid%> from mod_object as obj where obj.ID=objid) as name"
		);
		
		if( $response ) foreach( $response as $event ) {
			
			$start 	= $event['date'];
			$end	= date( 'Y-m-d H:i:s', strtotime( $event['date'] ) + ( $event['span'] * 60 ));
			
			if( $now > $start && $now < $end ) {
		
				$Cal 	= new OBJECT( $DB->type_r( $event['otype'] ), $event['objid'] );
				$nodes[] = "<li><a class='name' href='" . $Cal->uri() . "' title='" .$Cal->get( 'name' ) . "'>" .$Cal->get( 'name' ) . "</a></li>";
			
			}			
		}
		return $nodes;
	}
	
	
	private function render_li( $title, $uri = null, $media = null, $opt = null )	{
	
		if( $uri ) {
			$title = "<a href='$uri'>$title</a>";
			if( $media ) $media = "<a href='$uri'>$media</a>";
		}
	
		return "<li>$media$title$opt</li>";
		
	}

} 

?>


